import { ILoadMapTypeAll } from '@/common';
import { getBaseUrl } from '@/services/urlconst';
import { getCompanyID, getPosIDs, getTenantID, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';

let baseUrl = getBaseUrl();
// let baseUrl = '/wenchuang';

/**
 * 获取资产类别
 * @param getAssetClassCallback
 * @returns
 */
const getAssetClassService = (getAssetClassCallback: (res: any) => void) => {
  const getAssetClass = useRequest(
    (id: any) => {
      let personUrl = `${baseUrl}/assetInfo/getAssetType/${id}`;
      const body = {
        // id: id,
      };

      return requestFunComponFiex('GET', personUrl, body);
    },
    {
      manual: true,
      onSuccess: (res) => {
        getAssetClassCallback(res);
      },
    },
  );
  return {
    getAssetClassRun: getAssetClass?.run,
    getAssetClassLoading: getAssetClass?.loading,
  };
};

/**
 * 查看资产详情
 * @param callback 接口回调
 * @returns
 */
const detailAssetMaterielService = (callback: (res: any, clickType: boolean) => void) => {
  const detailAssetMateriel = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/assetInfo/detail`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, true);
      },
    },
  );
  return {
    detailAssetMaterielRun: detailAssetMateriel?.run,
    detailAssetMaterielLoading: detailAssetMateriel?.loading,
  };
};

/**
 * 保存资产
 * @param callback 接口回调
 * @returns
 */
const addAssetMaterielService = (callback: (res: any, clickType: boolean) => void) => {
  const addAssetMateriel = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        // state: `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1000`,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        // ownerID: getUserID(),
        // ownPositionID: getPosIDs(),
        // ownDepartmentID: getUserDepID(),
        // ownCompanyID: getCompanyID(),
        // dutyerID: getUserID(),
        // dutyPositionID: getPosIDs(),
        // dutyDepartmentID: getUserDepID(),
        // dutyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/assetMaterial/add`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, true);
      },
    },
  );
  return {
    addAssetMaterielRun: addAssetMateriel?.run,
    addAssetMaterielLoading: addAssetMateriel?.loading,
  };
};

/**
 * 编辑资产
 * @param callback 接口回调
 * @returns
 */
const editAssetMaterielService = (callback: (res: any, clickType: boolean) => void) => {
  const editAssetMateriel = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/assetInfo/edit`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    editAssetMaterielRun: editAssetMateriel?.run,
    editAssetMaterielLoading: editAssetMateriel?.loading,
  };
};

export { addAssetMaterielService, editAssetMaterielService, detailAssetMaterielService, getAssetClassService };
